package org.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.example.common.LoginVo;
import org.example.common.R;
import org.example.common.StatusCode;
import org.example.mapper.AttractionsMapper;
import org.example.pojo.entity.Attractions;
import org.example.pojo.entity.SysUser;
import org.example.pojo.entity.SysUserRole;
import org.example.pojo.vo.AttractionsVo;
import org.example.pojo.vo.UserVo;
import org.example.service.AttractionsService;
import org.example.service.SysUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static org.example.utils.securityContextUserInfo.getContextUserInfo;

@Service
public class AttractionsServiceImpl extends ServiceImpl<AttractionsMapper, Attractions>
        implements AttractionsService {
    @Autowired
    SysUserService sysUserService;

    @Override
    public Page<AttractionsVo> pageList(Integer page, Integer size, String scenicName, String scenicAddress, String status, String openTime, String closeTime) {
        Page<Attractions> pages = new Page<>(page, size);
        LambdaQueryWrapper<Attractions> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNoneBlank(scenicName)) {
            wrapper.like(Attractions::getScenicName, scenicName);
        }
        if (StringUtils.isNoneBlank(scenicAddress)) {
            wrapper.like(Attractions::getScenicAddress, scenicAddress);
        }
        if (StringUtils.isNoneBlank(status)) {
            wrapper.eq(Attractions::getStatus, status);
        }
        if (StringUtils.isNoneBlank(openTime) && StringUtils.isNoneBlank(closeTime)) {
            wrapper.between(Attractions::getOpenTime, openTime, closeTime);
        }
        List<AttractionsVo> attractionsVoList = new ArrayList<>();
        Page<Attractions> attractionsPage = baseMapper.selectPage(pages, wrapper);
        attractionsPage.getRecords().forEach(attractions -> {
            SysUser user = sysUserService.getById(attractions.getCreateId());
            String userName = user.getUserName();
            AttractionsVo attractionsVo = new AttractionsVo();
            BeanUtils.copyProperties(attractions, attractionsVo);
            attractionsVo.setUserName(userName);
            attractionsVoList.add(attractionsVo);
        });
        Page<AttractionsVo> attractionsVoPage = new Page<>();
        attractionsVoPage.setRecords(attractionsVoList);
        attractionsVoPage.setPages(attractionsPage.getPages());
        attractionsVoPage.setSize(attractionsPage.getSize());
        attractionsVoPage.setTotal(attractionsPage.getTotal());
        return attractionsVoPage;

    }

    @Override
    public R getScenicDetail(String scenicId) {
        return R.ok(this.getById(scenicId)) ;
    }

    @Override
    public R saveUser(Attractions attractions) {
        LambdaQueryWrapper<Attractions> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Attractions::getScenicName, attractions.getScenicName());
        Attractions attractionsOne = this.getOne(wrapper);
        if (attractionsOne != null) {
            return R.error(StatusCode.ATTR_ALREADY_EXIST);
        }
        LoginVo userInfo = getContextUserInfo();
        attractions.setCreateTime(LocalDateTime.now());
        attractions.setUpdateTime(LocalDateTime.now());
        attractions.setDelFlag("0");
        attractions.setType(2);
        attractions.setUpdateId(Integer.valueOf(userInfo.getUserId()));
        attractions.setCreateId(Integer.valueOf(userInfo.getUserId()));
        //保存景点基本信息
        this.save(attractions);
        return R.ok("操作成功");
    }
    @Override
    public R updateAttr(Attractions attractions) {
        /*LambdaQueryWrapper<Attractions> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Attractions::getScenicName, attractions.getScenicName());
        Attractions attractionsOne = this.getOne(wrapper);
        if (attractionsOne != null) {
            return R.error(StatusCode.ATTR_ALREADY_EXIST);
        }*/
        LoginVo userInfo = getContextUserInfo();
        attractions.setUpdateTime(LocalDateTime.now());
        attractions.setUpdateId(Integer.valueOf(userInfo.getUserId()));
        //更新景点基本信息
        this.updateById(attractions);
        return R.ok("操作成功");
    }
}
